package com.wan.data.redis.jedis;

import cn.hutool.core.lang.Assert;
import com.wan.core.shutdown.ShutdownManager;
import com.wan.util.ObjectUtil;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.UnifiedJedis;

public class JedisManager {

    private static UnifiedJedis jedis;

    public static UnifiedJedis getJedis(){
        Assert.notNull(jedis,"jedis没有初始化");
        return jedis;
    }

    public synchronized static void load(JedisSetting jedisSetting){
        JedisMode jedisMode;
        try {
            jedisMode = JedisMode.valueOf(jedisSetting.getMode());
        }catch (Exception e){
            if(!ObjectUtil.isEmpty(jedisSetting.getMode())){
                throw new RuntimeException("jedis的mode仅支持:" + ObjectUtil.enumString(JedisMode.class) + ",当前:" + jedisSetting.getMode(),e);
            }
            jedisMode = JedisMode.standalone;
        }
        jedis = jedisMode.createJedis(jedisSetting);
        jedis.sendCommand(Protocol.Command.PING);
        ShutdownManager.dependOn(jedis::close,"logger");
    }

}
